package com.qc.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.qc.common.StringUtil;
import com.qc.dao.TemperamentDao;
import com.qc.entity.TemperamentEntity;

@SuppressWarnings({ "unchecked", "rawtypes" })
@Repository("temperamentDao")
public class TemperamentDaoImpl implements TemperamentDao{
	
	@Resource(name="jdbcTemplate")
	private JdbcTemplate jdbcTemplate;

	@Override
	public int getListTemperamentCount(String name) {
	
		String sql = "select count(*) from t_temperament where 1 = 1 and if_del = '0' ";
	        
        if (StringUtil.isNotEmpty(name) && !"undefined".equals(name) ) {
            sql += " and name like '%"+name+"%' ";
        }
        return jdbcTemplate.queryForInt(sql);
	}

	@Override
	public List<TemperamentEntity> getAllTemperament(String name, int pageNo, int pageSize) {

		int beginRow = (pageNo - 1)* pageSize;
        
        String sql = "select b_id,name,sex from t_temperament where 1 = 1 and if_del = '0' ";
        
        if (StringUtil.isNotEmpty(name) && !"undefined".equals(name) ) {
            sql += " and name like '%"+name+"%' ";
        }
        
        sql += " order by created_time DESC ";
        sql += " LIMIT "+ String.valueOf(beginRow)+ "," + pageSize;
        
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper(TemperamentEntity.class));
	}

	@Override
	public TemperamentEntity getBossJobId(long bId) {
		
		String sql = "select b_id,name,sex from t_temperament where 1 = 1  and if_del = '0' ";
        
        if (bId != 0) {
            sql += " and b_id = '"+bId+"' ";
        }
        return (TemperamentEntity) jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper(TemperamentEntity.class));
	}

	@Override
	public int saveTemperament(TemperamentEntity temperament) {
		
		String sql = "insert into t_temperament(name,sex) values(?,?)";
		
        return jdbcTemplate.update(sql,new Object[]{temperament.getName(),temperament.getSex()});
	}

	@Override
	public int deleteTemperamentId(long id) {

		String sql = "update t_temperament set if_del = '1' where b_id = " +id;
		
	    return jdbcTemplate.update(sql);
	}

	@Override
	public int updateTemperamentId(TemperamentEntity temperament) {
		
		String sql = "update t_temperament set name = ? , sex = ? where b_id = ? ";
		
        return jdbcTemplate.update(sql,new Object[]{temperament.getName(),temperament.getSex(),temperament.getbId()});
	}

	@Override
	public List<TemperamentEntity> listAllTemperament(String sex) {
		
		String sql = "select b_id,name,sex from t_temperament where 1 = 1 and if_del = '0'";
		
		if (StringUtil.isNotEmpty(sex)) {
            sql += " and sex = '"+sex+"' ";
        }
		
		return jdbcTemplate.query(sql, new BeanPropertyRowMapper(TemperamentEntity.class));
	}

}
